<template>
    <el-empty v-if="empty.display" :description="empty.mark">
        <el-button @click="doLogin">重试</el-button>
    </el-empty>
    <div v-loading="loading"
         v-else
         element-loading-text="登录中，请稍后..." style="width: 100%;height: 100%">
    </div>
</template>
<script setup type="ts">
import {onMounted, reactive, ref} from "vue";
import router from "@/router";
import {getToken} from "@/api/api.token";

const loading = ref(true);
const empty = reactive({
    display: false,
    mark: ''
});

const doLogin = () => {
    localStorage.cleanToken();
    window.location.reload();
}

onMounted(() => {
    const params = {
        callback: window.location.origin + "/callback",
        code: router.currentRoute.value.query.code
    }
    getToken(params).then(res => {
        //  如果认真成功
        loading.value = false;
        if (res && res.data) {
            // 设置token TODO
            localStorage.setToken(res.data);
            // 跳转到指定页面 TODO
            router.push({name: "home"})
        } else {
            empty.display = true;
            empty.mark = res.mark;
        }
    })
})
</script>
